<?php
if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$Inside= new SoapClient(null,array(
        "location" => "http://localhost/open/openService/pft_insideMX.php",
        "uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
);

$txt = json_encode($_REQUEST); 
logfile($txt.'|'.$_REQUEST['remark'],'ctrip_Return_order');

$pft_order_id = $_REQUEST['distributor_order_id']; //票付通订单号
$ctrip_order_id = $_REQUEST['ctrip_order_id']; //携程订单号
$ctrip_order_status = $_REQUEST['order_status']; //携程订单状态0-下单成功，1-下单失败，2-退单成功，3-退单失败

if($ctrip_order_status == 0 && $pft_order_id){ //下单成功
	$selorder = "update all_api_order set apiOrder='$ctrip_order_id',handleStatus=0 where coopB=2 and oStatus= 0 and tempOrder='$pft_order_id' limit 1";
	$GLOBALS['le']->query($selorder);
	logfile($pft_order_id.'|'.$ctrip_order_id,'ctrip_submitorder_success');
	$result = 'true';
	$err_code = 0;
	$err_msg = '成功';
}elseif($ctrip_order_status == 1 && $pft_order_id){ //下单失败
	$selorder = "update all_api_order  set handleStatus=1 where coopB=2 and oStatus= 0 and tempOrder='$pft_order_id' limit 1";
	$GLOBALS['le']->query($selorder);
	logfile($pft_order_id.'|'.$ctrip_order_id,'ctrip_submitorder_fail');
	$result = 'true';
	$err_code = 0;
	$err_msg = '成功';
}elseif($ctrip_order_status == 2 && $pft_order_id){ //退单成功
	$updateorder = "update all_api_order set oStatus=3,handleStatus=0 where coopB=2 and tempOrder='$pft_order_id' and apiOrder='$ctrip_order_id' limit 1";
	$GLOBALS['le']->query($updateorder);
	logfile($pft_order_id.'|'.$ctrip_order_id,'ctrip_cancel_success');
	//收到退单成功后取消票付通订单
	$selorder = "select pftOrder from all_api_order where apiOrder='$ctrip_order_id' limit 1";
	$GLOBALS['le']->query($selorder);
	$GLOBALS['le']->fetch_assoc();
	$pftOrder = $GLOBALS['le']->f('pftOrder');
	$Cancel = $Inside->Order_Change_Pro($pftOrder,0,0,0,0,"","","");
	logfile($pftOrder.'|'.$Cancel,'ctrip_cancel_order');
	if($Cancel == 100){
		$result = 'true';
		$err_code = 0;
		$err_msg = '成功';
	}
	else{
		$result = 'fail';
		$err_code = -1;
		$err_msg = '失败';
	}
}elseif($ctrip_order_status == 3 && $pft_order_id){ //退单失败
	$selorder = "update all_api_order set oStatus=3,handleStatus=1 where coopB=2 and tempOrder='$pft_order_id' and apiOrder='$ctrip_order_id' limit 1";
	$GLOBALS['le']->query($selorder);
	logfile($pft_order_id.'|'.$ctrip_order_id,'ctrip_cancel_fail');
	$result = 'true';
	$err_code = 0;
	$err_msg = '成功';
}else{
	$result = 'fail';
	$err_code = -1;
	$err_msg = '操作失败';
}

$retxt = array(
	'is_success' => $result,
	'err_code'	=> $err_code,
	'err_msg'	=> $err_msg
);
echo json_encode($retxt);
//以下是函数部分
function logfile($word,$path){
	$filepath= '/var/www/html/new/d/logs/api/ctrip/'.$path.'.txt';
	if(file_exists($filepath)){
		$word = date("Y-m-d H:i:s")."|".$word;
		$word = mb_convert_encoding($word, 'GBK', 'UTF-8');
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
		{
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
			$handler=fopen($filepath,'w');
			flock($handler, LOCK_EX) ;
			fwrite($handler,"\n".$word);
			flock($handler, LOCK_UN);
			fclose($handler);
		}
		else
		{
			$fp = fopen('/var/www/html/new/d/logs/api/ctrip/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}else{
		$fp = fopen('/var/www/html/new/d/logs/api/ctrip/'.$path.'.txt',"a");
		flock($fp, LOCK_EX);
		fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}
?>